<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Contracts\Support\MessageBag;
use Response;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function makeResponse($result, $message, $code)
    {
        return [
            'status_code' => $code,
            'data'        => $result,
            'message'     => $message,
        ];
    }

    public function failResponse($message, $code = 400)
    {
        if ($message instanceof MessageBag) {
            $message = formatErrors($message);
        }
        return Response::json($this->makeResponse([], $message, $code), 200, [], JSON_UNESCAPED_UNICODE);
    }

    public function successResponse($result = [], $message = "操作成功", $header = 200)
    {
        return Response::json($this->makeResponse($result, $message, $header), $header, [], JSON_UNESCAPED_UNICODE);
    }
}
